﻿using Microsoft.EntityFrameworkCore.Migrations;

#nullable disable

namespace sqlserverdemo.Migrations
{
    /// <inheritdoc />
    public partial class InitialCreate : Migration
    {
        /// <inheritdoc />
        protected override void Up(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.CreateTable(
                name: "tdept",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    Addr = table.Column<string>(type: "nvarchar(max)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_tdept", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "trole",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_trole", x => x.Id);
                });

            migrationBuilder.CreateTable(
                name: "tuser",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Name = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Age = table.Column<int>(type: "int", nullable: true),
                    Mail = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Sex = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Addr = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    Phone = table.Column<string>(type: "nvarchar(max)", nullable: true),
                    DeptId = table.Column<long>(type: "bigint", nullable: true)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_tuser", x => x.Id);
                    table.ForeignKey(
                        name: "FK_tuser_tdept_DeptId",
                        column: x => x.DeptId,
                        principalTable: "tdept",
                        principalColumn: "Id");
                });

            migrationBuilder.CreateTable(
                name: "taddr",
                columns: table => new
                {
                    Id = table.Column<long>(type: "bigint", nullable: false)
                        .Annotation("SqlServer:Identity", "1, 1"),
                    Addr = table.Column<string>(type: "nvarchar(max)", nullable: false),
                    UserId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_taddr", x => x.Id);
                    table.ForeignKey(
                        name: "FK_taddr_tuser_UserId",
                        column: x => x.UserId,
                        principalTable: "tuser",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateTable(
                name: "tuser_role",
                columns: table => new
                {
                    RoleId = table.Column<long>(type: "bigint", nullable: false),
                    UserId = table.Column<long>(type: "bigint", nullable: false)
                },
                constraints: table =>
                {
                    table.PrimaryKey("PK_tuser_role", x => new { x.RoleId, x.UserId });
                    table.ForeignKey(
                        name: "FK_tuser_role_trole_RoleId",
                        column: x => x.RoleId,
                        principalTable: "trole",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                    table.ForeignKey(
                        name: "FK_tuser_role_tuser_UserId",
                        column: x => x.UserId,
                        principalTable: "tuser",
                        principalColumn: "Id",
                        onDelete: ReferentialAction.Cascade);
                });

            migrationBuilder.CreateIndex(
                name: "IX_taddr_UserId",
                table: "taddr",
                column: "UserId");

            migrationBuilder.CreateIndex(
                name: "IX_tuser_DeptId",
                table: "tuser",
                column: "DeptId",
                unique: true,
                filter: "[DeptId] IS NOT NULL");

            migrationBuilder.CreateIndex(
                name: "IX_tuser_role_UserId",
                table: "tuser_role",
                column: "UserId");
        }

        /// <inheritdoc />
        protected override void Down(MigrationBuilder migrationBuilder)
        {
            migrationBuilder.DropTable(
                name: "taddr");

            migrationBuilder.DropTable(
                name: "tuser_role");

            migrationBuilder.DropTable(
                name: "trole");

            migrationBuilder.DropTable(
                name: "tuser");

            migrationBuilder.DropTable(
                name: "tdept");
        }
    }
}
